package org.visualchina.elasticsearch.api.rest;


import com.alibaba.fastjson.JSON;
import org.apache.http.HttpEntity;
import org.apache.http.entity.ContentType;
import org.apache.http.nio.entity.NStringEntity;
import org.apache.http.util.EntityUtils;
import org.elasticsearch.client.Response;
import org.junit.Test;
import org.visualchina.elasticsearch.api.demo.XPackBaseDemo;
import org.visualchina.elasticsearch.api.model.UserModel;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;

/**
 * @auhthor lei.fang@shijue.me
 * @since . 2017-08-04
 */
public class RestQueryDemo extends XPackBaseDemo{

    @Test
    public void testCatApi() throws Exception {
        String method = "GET";
        String endpoint = "/_cat";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    @Test
    public void testCreateIndex() throws Exception {
        String method = "PUT";
        String endpoint = "/test-index";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 新增
     * @throws Exception
     */
    @Test
    public void testCreateDocument() throws Exception {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");
        Calendar c=Calendar.getInstance();
        c.add(Calendar.HOUR_OF_DAY, -8);//8小时前
        for(int i=1;i<10;i++){
            String method = "PUT";
            String endpoint = "/twitter/test/"+i;
            UserModel userModel = new UserModel();
            userModel.setUser("哈哈哈");
            userModel.setPost_date(simpleDateFormat.format(c.getTime()));
            userModel.setReal_date(simpleDateFormat.format(new Date()));
            userModel.setMessage("中文"+i+"就没名片翁我就怕睫毛膏，库");
            String json = JSON.toJSONString(userModel);
            HttpEntity entity = new NStringEntity(json, ContentType.APPLICATION_JSON);
            Response response = restClient.performRequest(method,endpoint, Collections.<String, String>emptyMap(),entity);
            System.out.println(EntityUtils.toString(response.getEntity()));
        }
    }

    /**
     * 根据id查询
     * @throws Exception
     */
    @Test
    public void testGetDocument() throws Exception {
        String method = "GET";
        String endpoint = "/twitter/tweet/2";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     * 分词
     * @throws Exception
     */
    @Test
    public void testIK() throws Exception {
        String method = "GET";
        String endpoint = "/index/_analyze?analyzer=ik_max_word&text=中华人民共和国";
        Response response = restClient.performRequest(method,endpoint);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    @Test
    public void testQueryAll() throws Exception {
        String method = "POST";
        String endpoint = "/twitter/tweet/_search";
        HttpEntity entity = new NStringEntity("{\n" +
                "  \"query\": {\n" +
                "    \"match_all\": {}\n" +
                "  }\n" +
                "}", ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     *  根据字段查询
     * @throws Exception
     */
    @Test
    public void testQueryByField() throws Exception {
        String method = "POST";
        String endpoint = "/twitter/test/_search";
        HttpEntity entity = new NStringEntity("{\n" +
                "  \"query\": {\n" +
                "    \"match\": {\n" +
                "      \"message\": \"中文\"\n" +
                "    }\n" +
                "  }\n" +
                "}", ContentType.APPLICATION_JSON);

        Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    /**
     *  根据id更新数据
     * @throws Exception
     */
    @Test
    public void testUpdateByScript() throws Exception {
        String method = "POST";
        String endpoint = "/twitter/tweet/2/_update";
        HttpEntity entity = new NStringEntity("{\n" +
                "  \"doc\": {\n" +
                "    \"user\":\"你猜我是谁\"\n" +
                "  }\n" +
                "}", ContentType.APPLICATION_JSON);
        Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

    @Test
    public void testGeoBoundingBox() throws IOException {
        String method = "POST";
        String endpoint = "/attractions/restaurant/_search";
        HttpEntity entity = new NStringEntity("{\n" +
                "  \"query\": {\n" +
                "    \"match_all\": {}\n" +
                "  },\n" +
                "  \"post_filter\": {\n" +
                "    \"geo_bounding_box\": {\n" +
                "      \"location\": {\n" +
                "        \"top_left\": {\n" +
                "          \"lat\": 39.990481,\n" +
                "          \"lon\": 116.277144\n" +
                "        },\n" +
                "        \"bottom_right\": {\n" +
                "          \"lat\": 39.927323,\n" +
                "          \"lon\": 116.405638\n" +
                "        }\n" +
                "      }\n" +
                "    }\n" +
                "  }\n" +
                "}", ContentType.APPLICATION_JSON);
        Response response = restClient.performRequest(method,endpoint,Collections.<String, String>emptyMap(),entity);
        System.out.println(EntityUtils.toString(response.getEntity()));
    }

}
